package com.itheima.service.impl;

/*
预约设置服务
* */

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public void add(List<OrderSetting> data) {
        if (data!=null || data.size()>0){
            for (OrderSetting datum : data) {
                Long countByOrderDate=orderSettingDao.findCountByOrderDate(datum.getOrderDate());
                if (countByOrderDate>0){
                    orderSettingDao.editNumberByOrderDate(datum);
                }else{
                    orderSettingDao.add(datum);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin=date+"-1";
        String end=date+"-31";
        Map<String,String> map=new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);

        List<OrderSetting>list=orderSettingDao.getOrderSettingByMonth(map);
        List<Map> list1=new ArrayList<>();
        if (list!=null && list.size()>0){
            for (OrderSetting setting : list) {
                Map<String,Object> map1= new HashMap<>();
                map1.put("date",setting.getOrderDate().getDate());
                map1.put("number",setting.getNumber());
                map1.put("reservations",setting.getReservations());
                list1.add(map1);
            }
        }
        return list1;
    }

    //根据日期设置对应的预约设置数据
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate=orderSetting.getOrderDate();
        //根据日期查询是否已经进行预约设置
        long count=orderSettingDao.findCountByOrderDate(orderDate);
        if (count>0){
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            orderSettingDao.add(orderSetting);
        }
    }
}
